package com.qcu.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.qcu.annotation.LimitRate;
import com.qcu.annotation.RequireAuthorization;
import com.qcu.dto.StaffAccountDto;
import com.qcu.entity.StaffAccount;
import com.qcu.service.DoctorMenuService;
import com.qcu.service.StaffAccountService;
import com.qcu.util.Response;
import com.qcu.util.ResponseCode;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;


/**
 * @Name: MyComputer
 * @ClassName StaffAccountController
 * @Description: 后台员工管理
 * @date 2025/2/6 9:22
 */

@RestController
@RequestMapping("/admin/staff")
public class StaffAccountController {

    @Resource
    StaffAccountService staffAccountService;

    @Resource
    DoctorMenuService doctorMenuService;

    @GetMapping("/getDoctorMenu")
    @RequireAuthorization({"ADMIN"})
    public Response getDoctorMenu(){
        return Response.success(doctorMenuService.list());
    }

    /**
     * 保存员工信息
     * @param staffAccountDto
     * @return
     */
    @PostMapping("/save")
    @RequireAuthorization({"ADMIN"})
    public Response save(@RequestBody StaffAccountDto staffAccountDto){
        return staffAccountService.saveStaff(staffAccountDto);
    }

    /**
     * 修改员工信息
     * @param staffAccountDto
     * @return
     */
    @PutMapping("/update")
    @RequireAuthorization({"ADMIN"})
    public Response update(@RequestBody StaffAccountDto staffAccountDto){
        return staffAccountService.updateStaff(staffAccountDto);
    }


    /**
     * 删除员工信息
     * @param id
     * @return
     */
    @DeleteMapping("/remove/{id}")
    @RequireAuthorization({"ADMIN"})
    public Response remove(@PathVariable Integer id){
        return staffAccountService.removeById(id) ? Response.success() : Response.failure(ResponseCode.FAILURE, "删除失败");
    }

    /**
     * 根据姓名查询员工信息
     * @param firstname
     * @param lastname
     * @return
     */
    @GetMapping("/get/{firstname}/{lastname}")
    @RequireAuthorization({"ADMIN"})
    @LimitRate(key = "getStaffAccount",permitsPerSecond = 2,expire = 10,msg = "八嘎雅鹿，你滴点那么快滴干什么滴干活...")
    public Response get(@PathVariable String firstname, @PathVariable String lastname){
        LambdaQueryWrapper<StaffAccount> staffAccountLambdaQueryWrapper = new LambdaQueryWrapper<>();
        staffAccountLambdaQueryWrapper.eq(StaffAccount::getFirstName, firstname );
        staffAccountLambdaQueryWrapper.eq(StaffAccount::getLastName, lastname );
        StaffAccount one = staffAccountService.getOne(staffAccountLambdaQueryWrapper);
        return one != null ? Response.success(one) : Response.failure(ResponseCode.FAILURE, "查询失败");
    }

    /**
     * 查询所有员工信息
     * @return
     */
    @GetMapping("/get")
    @RequireAuthorization({"ADMIN"})
    public Response get(){
        LambdaQueryWrapper<StaffAccount> staffAccountLambdaQueryWrapper = new LambdaQueryWrapper<>();
        staffAccountLambdaQueryWrapper.eq(StaffAccount::getRole, "STAFF").or().eq(StaffAccount::getRole, "PLAIN");
        return Response.success(staffAccountService.list(staffAccountLambdaQueryWrapper));
    }

    @GetMapping("/getInfo/{username}")
    public Response getInfo(@PathVariable String username){
        LambdaQueryWrapper<StaffAccount> staffAccountLambdaQueryWrapper = new LambdaQueryWrapper<>();
        staffAccountLambdaQueryWrapper.eq(StaffAccount::getRole, "STAFF").or().eq(StaffAccount::getRole, "PLAIN");
        return Response.success(staffAccountService.list(staffAccountLambdaQueryWrapper));
    }

}
